<template>
  <div class="login-wrap">
    <div class="cover"></div>
    <div class="login">
      <div class="login-body">
        <header class="header">
          <img class="logo" src="@/assets/logo.png" alt="" />
          <h1 class="title">vue admin</h1>
        </header>
        <el-form
          class="login-form"
          ref="form"
          :model="formValue"
          :rules="rules"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              type="text"
              v-model="formValue.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="formValue.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item class="submit-item">
            <el-button class="submit" type="primary" @click="submitAction"
              >提交</el-button
            >
          </el-form-item>
          <el-form-item class="info">
            <el-link type="primary">忘记密码</el-link>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { Form, FormItem, Input, Button, Link, Message } from "element-ui";
import MD5 from "js-md5";
import axios from "@/api/axios";
export default {
  components: {
    [Form.name]: Form,
    [FormItem.name]: FormItem,
    [Input.name]: Input,
    [Button.name]: Button,
    [Link.name]: Link,
  },
  data() {
    return {
      formValue: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 16, message: "密码长度为8～16", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 登录表单的提交事件
    submitAction() {
      // 进行校验
      this.$refs.form.validate(async (result) => {
        if (result) {
          try {
            //登录
            const response = await axios.post("/login", {
              username: this.formValue.username,
              password: MD5(this.formValue.password)
            });
            console.log(response);
            if(response.status === 200){
              localStorage.setItem('TOKEN', response.data.token);
              
              this.$router.replace('/');
            }
          } catch (error) {
            console.log('fail',error);
            Message.error('用户名或密码错误');
          }
          
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login-wrap,
.cover,
.login {
  width: 100%;
  height: 100%;
}
.login-wrap {
  position: relative;
}
.cover,
.login {
  position: absolute;
  top: 0;
  left: 0;
}
.cover {
  background-image: url(/images/bg.png);
  background-size: cover;
  filter: blur(3px);
}
.login {
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-body {
  width: 400px;
  height: 500px;
  background-color: #fff;
  box-shadow: 0 20px 40px 0 rgba($color: #000000, $alpha: 1);
}
.header {
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .logo {
    width: 100px;
  }
  .title {
    padding-top: 20px;
    font-size: 24px;
    font-weight: bold;
  }
}
.login-form {
  padding: 0 40px;
  .submit-item {
    margin-bottom: 0px !important;
  }
  .submit {
    width: 100%;
  }
  .info {
    padding: 10px;
    text-align: center;
  }
}
</style>

<style lang="scss">
.login-form {
  .el-form-item {
    margin-bottom: 20px;
  }
  .el-form-item__label {
    line-height: 30px;
  }
}
</style>